<?php
defined('SYSPATH') OR die('No direct access allowed.');
/**
 * pendidikan.php
 * @author ranuandra
 *
 * 30 Apr 2012 12:55:38
 *
 * 
 */

class Controller_Admin_Pendidikan extends Kohana_Admin
{
	
	public function action_index()
	{
		$datas = ORM::factory('mstpendidikan')->find_all();
		$view = View::factory('admin/pendidikan/index');
		$view->datas = $datas;
		$this->template->container = $view;
	}
	
	public function action_new()
	{
		$form = ORM::factory('mstpendidikan')->getForm();
		$view = View::factory('admin/pendidikan/form');
		$view->form = $form;
		$this->template->container = $view;
	}
	
	public function action_edit()
	{
		$id = Request::current()->param('id');
		if($id != "")
		{
			$view = View::factory('admin/pendidikan/form');
			$data = ORM::factory('mstpendidikan')->where('id', '=', $id)->find()->as_array();
			
			$view->form = $data;
			$view->id = $id;
			
			$this->template->container = $view;
			
			
		}		
	}
	
	public function action_save()
	{
		if(isset($_POST['submit']))
		{
			$db = Database::instance();
			$db->begin();
			
			$id = Request::current()->post('id');
			
			
			try 
			{
				if($id == "")
				{
					$data = ORM::factory('mstpendidikan');
					$data->create_data($_POST);
					//Request::current()->redirect(URL::base().'admin/aplikasi');
					//exit;
				}
				else 
				{
					$data = ORM::factory('mstpendidikan', $id);
					$data->pendidikan = Request::current()->post('pendidikan');
					$data->update();
				}
				
				$db->commit();
				Request::current()->redirect(URL::base()."admin/pendidikan");

			}
			catch(ORM_Validation_Exception $e)
			{
				$db->rollback();
				$view = View::factory('admin/pendidikan/form');
				$view->errors = $e->errors('models');
				$view->form = Request::current()->post();
				//print_r($errors);
				$this->template->container = $view;
			}
		}		
	}
}